import {createRouter, createWebHistory} from 'vue-router'
import {useUserStore} from '@/stores/user'
// @ts-ignore
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

NProgress.configure({showSpinner: false});

// 标记是否是页面刷新
let isPageRefreshing = true;

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            redirect: '/admin/login'
        },
        {
            path: '/admin/login',
            name: 'adminLogin',
            component: () => import('@/views/login/index.vue'),
            meta: {
                title: '登录',
                requiresAuth: false
            }
        },
        {
            path: '/admin',
            name: 'admin',
            component: () => import('@/components/layout/index.vue'),
            children: [{
                path: 'index',
                name: 'adminIndex',
                component: () => import('@/views/home/index.vue'),
                meta: {
                    title: '首页',
                    requiresAuth: true,
                    icon: 'HomeFilled'
                }
            }, {
                path: 'template/table',
                name: 'template/table',
                component: () => import('@/views/template/table/index.vue'),
                meta: {
                    title: '表格示例',
                    requiresAuth: false,
                    icon: 'List'
                },
            }, {
                path: 'template/icon',
                name: 'template/icon',
                component: () => import('@/views/template/icon/index.vue'),
                meta: {
                    title: '图标示例',
                    requiresAuth: false,
                    icon: 'Document'
                },
            }, {
                path: 'system/user',
                name: 'systemUser',
                component: () => import('@/views/system/user/index.vue'),
                meta: {
                    title: '用户管理',
                    requiresAuth: true,
                    icon: 'User'
                },
            }, {
                path: 'system/dept',
                name: 'systemDept',
                component: () => import('@/views/system/dept/index.vue'),
                meta: {
                    title: '部门管理',
                    requiresAuth: true,
                    icon: 'List'
                }
            }, {
                path: 'system/post',
                name: 'systemPost',
                component: () => import('@/views/system/post/index.vue'),
                meta: {
                    title: '岗位管理',
                    requiresAuth: true,
                    icon: 'Document'
                }
            }, {
                path: 'system/role',
                name: 'systemRole',
                component: () => import('@/views/system/role/index.vue'),
                meta: {
                    title: '角色管理',
                    requiresAuth: true,
                    icon: 'Lock'
                }
            }, {
                path: 'system/menu',
                name: 'systemMenu',
                component: () => import('@/views/system/menu/index.vue'),
                meta: {
                    title: '菜单管理',
                    requiresAuth: true,
                    icon: 'List'
                }
            }, {
                path: 'system/dict',
                name: 'systemDict',
                component: () => import('@/views/system/dict/index.vue'),
                meta: {
                    requiresAuth: true,
                    title: '字典管理',
                    icon: 'Document'
                }
            }, {
                path: 'system/dict/data/:type',
                name: 'systemDictData',
                component: () => import('@/views/system/dict/data.vue'),
                meta: {
                    requiresAuth: true,
                    title: '字典数据'
                }
            }, {
                path: 'system/config',
                name: 'systemConfig',
                component: () => import('@/views/system/config/index.vue'),
                meta: {
                    title: '系统配置',
                    requiresAuth: true,
                    icon: 'Setting'
                }
            },
                // 添加监控相关路由
                {
                    path: 'monitor/job',
                    name: 'monitorJob',
                    component: () => import('@/views/monitor/job/index.vue'),
                    meta: {
                        title: '定时任务',
                        requiresAuth: true,
                        icon: 'Timer'
                    },
                }, {
                    path: 'monitor/cache',
                    name: 'monitorCache',
                    component: () => import('@/views/monitor/cache/index.vue'),
                    meta: {
                        title: '数据缓存',
                        requiresAuth: true,
                        icon: 'DataAnalysis'
                    },
                }, {
                    path: 'monitor/online',
                    name: 'monitorOnline',
                    component: () => import('@/views/monitor/online/index.vue'),
                    meta: {
                        title: '在线用户',
                        requiresAuth: true,
                        icon: 'User'
                    },
                }, {
                    path: 'monitor/sql',
                    name: 'monitorSql',
                    component: () => import('@/views/monitor/sql/index.vue'),
                    meta: {
                        title: 'SQL监控',
                        requiresAuth: true,
                        icon: 'DataAnalysis'
                    },
                },
                // 添加日志管理相关路由
                {
                    path: 'log/operlog',
                    name: 'logOperlog',
                    component: () => import('@/views/log/operlog/index.vue'),
                    meta: {
                        title: '操作日志',
                        requiresAuth: true,
                        icon: 'Document'
                    },
                }, {
                    path: 'log/loginlog',
                    name: 'logLoginlog',
                    component: () => import('@/views/log/loginlog/index.vue'),
                    meta: {
                        title: '登录日志',
                        requiresAuth: true,
                        icon: 'List'
                    },
                },
                // 添加系统工具相关路由
                {
                    path: 'tool/file',
                    name: 'toolFile',
                    component: () => import('@/views/tool/file/index.vue'),
                    meta: {
                        title: '文件管理',
                        requiresAuth: true,
                        icon: 'Files'
                    },
                }, {
                    path: 'tool/gen',
                    name: 'toolGen',
                    component: () => import('@/views/tool/gen/index.vue'),
                    meta: {
                        title: '代码生成',
                        requiresAuth: true,
                        icon: 'Tools'
                    },
                }, {
                    path: 'system/profile',
                    name: 'systemProfile',
                    component: () => import('@/views/profile/index.vue'),
                    meta: {
                        title: '个人中心',
                        requiresAuth: true,
                        icon: 'User'
                    },
                }, {
                    path: 'profile',
                    name: 'profile',
                    component: () => import('@/views/profile/index.vue'),
                    meta: {
                        title: '个人中心',
                        requiresAuth: true
                    },
                },
                {
                    path: 'external/github',
                    name: 'github',
                    component: () => import('@/views/iframe/IframePage.vue'),
                    meta: {
                        title: 'github',
                        requiresAuth: true,
                        url: 'https://github.com',
                        icon: 'Tools'
                    },
                },
                {
                    path: 'external/element-plus',
                    name: 'element',
                    component: () => import('@/views/iframe/IframePage.vue'),
                    meta: {
                        title: 'element-plus',
                        requiresAuth: true,
                        url: 'https://element-plus.org',
                        icon: 'Tools'
                    },
                }
            ]
        },
        {
            path: '/table-demo',
            name: 'tableDemo',
            component: () => import('@/views/template/table/index.vue'),
            meta: {
                title: '表格示例',
                requiresAuth: false
            }
        }
    ],
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
    NProgress.start();
    const userStore = useUserStore()
    document.title = to.meta.title ? `${to.meta.title} - 后台管理系统` : '后台管理系统'

    // 在路由元信息中标记是否是刷新加载
    to.meta.isRefreshing = isPageRefreshing;

    // 第一次导航后，标记为非刷新状态
    if (isPageRefreshing) {
        isPageRefreshing = false;
    }

    if (to.meta.requiresAuth && !userStore.token) {
        // 需要登录但用户未登录，重定向到登录页
        next({name: 'adminLogin'})
    } else {
        next()
    }
})

router.afterEach(() => {
    NProgress.done();
});

export default router
